/**
 * Created by enixjin on 1/3/17.
 */
import {AfterViewInit, OnInit} from "@angular/core";
import {Router} from "@angular/router";
import {User} from "../entity/User";
import {MessageService} from "../service/messageService";
import {DialogService} from "../service/dialogService";


export abstract class BaseAuthenticateComponent implements AfterViewInit, OnInit {
    loginUser: User;
    token: string;

    constructor(protected a_router: Router, public _dialogService?: DialogService) {
        this.loginUser = JSON.parse(localStorage.getItem("loginUser"));
        this.token = localStorage.getItem("token");
    }

    ngOnInit() {
        // do nothing now
    }

    ngAfterViewInit() {
        // do nothing now
    }

    updateLocalUser() {
        localStorage.setItem("loginUser", JSON.stringify(this.loginUser));
    }

    refreshLocalUser() {
        this.loginUser = JSON.parse(localStorage.getItem("loginUser"));
    }

    jumpToLogin() {
        this.a_router.navigate(["/login"]);
    }

    handleError(message) {
        // this.a_messageService.pushMessage({title: "Error", content: message, type: "error"});
        this._dialogService.pushDialogMsg({content: message, btnOKText: "确认"});
    }

    logout() {
        localStorage.clear();
    }

    handleSuccess(message) {
        // this.a_messageService.pushMessage({title: "Success", content: message, type: "success"});
        this._dialogService.pushDialogMsg({content: message, btnOKText: "确认"});
    }
}
